using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using NBear.Common;
using NBear.Data;

namespace NBear.Test.UnitTests
{
    /// <summary>
    /// Summary description for TestSimpleDbHelper
    /// </summary>
    [TestClass]
    public class TestSimpleDbHelper
    {
        public TestSimpleDbHelper()
        {
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) 
        {
            Database.Default = new Database(DbProviderFactory.CreateDbProvider(null, typeof(NBear.Data.SqlServer.SqlDbProvider).FullName, "Server=(local);Database=Northwind;Uid=sa;Pwd=sa"));
        }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestNonTransactionFunctions()
        {
            SimpleDbHelper.Delete(Database.Default, "Products", "ProductID = @ProductID", new object[] { 0 });

            SimpleDbHelper.Delete(Database.Default, "Products", "ProductID = @ProductID", new DbType[] { DbType.Int32 }, new object[] { 0 });

            SimpleDbHelper.ExecuteNonQuery(Database.Default, "delete from Products where ProductID = @ProductID", 0);

            DataSet ds = SimpleDbHelper.ExecuteProcedure(Database.Default, "CustOrdersOrders", new string[] { "CustomerID" }, new object[] { 1 });

            IDataReader reader = SimpleDbHelper.ExecuteProcedureReadOnly(Database.Default, "CustOrdersOrders", new string[] { "CustomerID" }, new object[] { 1 });
        }

        [TestMethod]
        public void TestBasicTransactionFunctions()
        {
            System.Data.Common.DbTransaction tran = Database.Default.BeginTransaction();

            SimpleDbHelper.Delete(Database.Default, "Order Details", "ProductID = @ProductID and OrderID = 10248", new object[] { 1 }, tran);
            SimpleDbHelper.Delete(Database.Default, "Products", "ProductID = @ProductID", new object[] { 0 }, tran);
            SimpleDbHelper.ExecuteNonQuery(Database.Default, "delete from Products where ProductID = @ProductID", tran, 0);
            SimpleDbHelper.ExecuteProcedure(Database.Default, "CustOrdersOrders", new string[] { "CustomerID" }, new object[] { 1 }, tran);
            SimpleDbHelper.ExecuteProcedureReadOnly(Database.Default, "CustOrdersOrders", new string[] { "CustomerID" }, new object[] { 1 }, tran).Close();

            tran.Commit();
        }

        [TestMethod]
        public void TestADO20TransactionFunctions()
        {
            using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required))
            {
                SimpleDbHelper.Delete(Database.Default, "Order Details", "ProductID = @ProductID and OrderID = 10248", new object[] { 1 });

                SimpleDbHelper.Delete(Database.Default, "Products", "ProductID = @ProductID", new object[] { 0 });

                SimpleDbHelper.Delete(Database.Default, "Products", "ProductID = @ProductID", new DbType[] { DbType.Int32 }, new object[] { 0 });

                SimpleDbHelper.ExecuteNonQuery(Database.Default, "delete from Products where ProductID = @ProductID", 0);

                SimpleDbHelper.ExecuteProcedure(Database.Default, "CustOrdersOrders", new string[] { "CustomerID" }, new object[] { 1 });

                SimpleDbHelper.ExecuteProcedureReadOnly(Database.Default, "CustOrdersOrders", new string[] { "CustomerID" }, new object[] { 1 });

                scope.Complete();
            }
        }
    }
}
